package com.qst.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.pojo.*;
import com.qst.service.ApplyService;
import com.qst.service.HouselistService;
import com.qst.service.UserlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller @RequestMapping("/apply") public class ApplyController {
	 @Autowired private UserlistService userlistService;
	 @Autowired private HouselistService houselistService;
	 @Autowired private ApplyService applyService;

	 // 申请看房
	 @RequestMapping("/applycheckuserlist") public String applycheckuserlist(HttpSession httpSession,
			 Model model, Integer id) {
		  User user1 = (User) httpSession.getAttribute("user");
		  Integer user_id = user1.getId();
		  Userlist list = userlistService.findhasuserlist(user_id);
		  if (list == null) {
				model.addAttribute("error", "applycheck");
				return "redirect:houselist.action";
		  } else {
				Houselist houselist = houselistService.findid(id);
				houselist.setStatus("申请中");
				houselistService.updatehousestatus(houselist);
				Integer userlist_id = list.getId();
				Apply apply = new Apply();
				apply.setHouse_id(houselist.getHouseid());
				apply.setAddress(houselist.getAddress());
				apply.setPrice(houselist.getPrice());
				apply.setArea(houselist.getArea());
				apply.setStatus("申请中");
				apply.setCreatedate(new Date());
				apply.setUserlist_id(userlist_id);
				applyService.insertapply(apply);
				model.addAttribute("error", "applysuccess");
				return "redirect:/houselist.action";
		  }

	 }

	 // 房东查看自己的申请看房列表，管理员查看所有申请
	 @RequestMapping("/findapplylist") public String findapplylist(Model model,
			 @RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer pageSize,
			 HttpSession httpSession) throws Exception {
		  PageHelper.startPage(page, pageSize);
		  User user = (User) httpSession.getAttribute("user");
		  Integer qid = user.getId();
		  List<Apply> applylist=new ArrayList<Apply>();
		  if ("admin".equals(user.getUsertype())) {
				applylist=applyService.findAllApplyList();
				System.out.println("admin~~~");
		  }else{
				applylist=applyService.findApplyList(qid);
				System.out.println("fangdong~~~");
		  }
		  PageInfo<Apply> p = new PageInfo<Apply>(applylist);
		  model.addAttribute("applylist", applylist);
		  model.addAttribute("p", p);
		  model.addAttribute("mainPage", "../apply/applylist.jsp");
		  return "common/main";
	 }

	 @RequestMapping("/applychangehousestatus") public String applychangehousestatus(
			 HttpSession httpSession, Model model, String house_id) throws Exception {
		  User user1 = (User) httpSession.getAttribute("user");
		  Integer user_id = user1.getId();
		  Userlist userlist = userlistService.findhasuserlist(user_id);
		  Houselist houselist = houselistService.findhouseid(house_id);
		  houselist.setStatus("已租赁");
		  houselistService.updatehousestatus(houselist);
		  Zulist zulist = new Zulist();
		  zulist.setHouse_id(house_id);
		  zulist.setPrice(houselist.getPrice());
		  zulist.setAddress(houselist.getAddress());

		  return "";
	 }

	 // 管理员拒绝看房申请
	 @RequestMapping("/refuseapply") public String refuseapply(String house_id, Model model) {
		  Houselist houselist = new Houselist();
		  houselist.setHouseid(house_id);
		  houselist.setStatus("未租赁");
		  applyService.refuseapply(houselist);

		  return "redirect:findapplylist.action";
	 }

	 // 租客查看自己的 看房申请
	 @RequestMapping("/getmyapply") public String getmyapply(Model model, HttpSession httpSession,
			 @RequestParam(required = false, defaultValue = "1") Integer page,
			 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
		  User user1 = (User) httpSession.getAttribute("user");
		  Userlist userlist = userlistService.findhasuserlist(user1.getId());
		  PageHelper.startPage(page, pageSize);
		  List<Userlist> list = userlistService.getmyapply(userlist.getId());
		  PageInfo<Userlist> p = new PageInfo<Userlist>(list);
		  model.addAttribute("userlist", list);
		  model.addAttribute("p", p);
		  model.addAttribute("mainPage", "../apply/myapply.jsp");
		  return "common/main";
	 }

}
